home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / cal-move.el < prev    next >
Lisp/Scheme  |  1996-01-20  |  12KB  |  316 lines

  1. ;;; cal-move.el --- calendar functions for movement in the calendar
  2.  
  3. ;; Copyright (C) 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  6. ;; Keywords: calendar
  7. ;; Human-Keywords: calendar
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This collection of functions implements movement in the calendar for
  29. ;; calendar.el.
  30.  
  31. ;; Comments, corrections, and improvements should be sent to
  32. ;;  Edward M. Reingold               Department of Computer Science
  33. ;;  (217) 333-6733                   University of Illinois at Urbana-Champaign
  34. ;;  reingold@cs.uiuc.edu             1304 West Springfield Avenue
  35. ;;                                   Urbana, Illinois 61801
  36.  
  37. ;;; Code:
  38.  
  39. (defun calendar-goto-today ()
  40.   "Reposition the calendar window so the current date is visible."
  41.   (interactive)
  42.   (let ((today (calendar-current-date)));; The date might have changed.
  43.     (if (not (calendar-date-is-visible-p today))
  44.         (generate-calendar-window)
  45.       (update-calendar-mode-line)
  46.       (calendar-cursor-to-visible-date today))))
  47.  
  48. (defun calendar-forward-month (arg)
  49.   "Move the cursor forward ARG months.
  50. Movement is backward if ARG is negative."
  51.   (interactive "p")
  52.   (calendar-cursor-to-nearest-date)
  53.   (let* ((cursor-date (calendar-cursor-to-date t))
  54.          (month (extract-calendar-month cursor-date))
  55.          (day (extract-calendar-day cursor-date))
  56.          (year (extract-calendar-year cursor-date)))
  57.     (increment-calendar-month month year arg)
  58.     (let ((last (calendar-last-day-of-month month year)))
  59.       (if (< last day)
  60.         (setq day last)))
  61.     ;; Put the new month on the screen, if needed, and go to the new date.
  62.     (let ((new-cursor-date (list month day year)))
  63.       (if (not (calendar-date-is-visible-p new-cursor-date))
  64.           (calendar-other-month month year))
  65.       (calendar-cursor-to-visible-date new-cursor-date))))
  66.  
  67. (defun calendar-forward-year (arg)
  68.   "Move the cursor forward by ARG years.
  69. Movement is backward if ARG is negative."
  70.   (interactive "p")
  71.   (calendar-forward-month (* 12 arg)))
  72.  
  73. (defun calendar-backward-month (arg)
  74.   "Move the cursor backward by ARG months.
  75. Movement is forward if ARG is negative."
  76.   (interactive "p")
  77.   (calendar-forward-month (- arg)))
  78.  
  79. (defun calendar-backward-year (arg)
  80.   "Move the cursor backward ARG years.
  81. Movement is forward is ARG is negative."
  82.   (interactive "p")
  83.   (calendar-forward-month (* -12 arg)))
  84.  
  85. (defun scroll-calendar-left (arg)
  86.   "Scroll the displayed calendar left by ARG months.
  87. If ARG is negative the calendar is scrolled right.  Maintains the relative
  88. position of the cursor with respect to the calendar as well as possible."
  89.   (interactive "p")
  90.   (calendar-cursor-to-nearest-date)
  91.   (let ((old-date (calendar-cursor-to-date))
  92.         (today (calendar-current-date)))
  93.     (if (/= arg 0)
  94.         (progn
  95.           (increment-calendar-month displayed-month displayed-year arg)
  96.           (generate-calendar-window displayed-month displayed-year)
  97.           (calendar-cursor-to-visible-date
  98.            (cond
  99.             ((calendar-date-is-visible-p old-date) old-date)
  100.             ((calendar-date-is-visible-p today) today)
  101.             (t (list displayed-month 1 displayed-year))))))))
  102.  
  103. (defun scroll-calendar-right (arg)
  104.   "Scroll the displayed calendar window right by ARG months.
  105. If ARG is negative the calendar is scrolled left.  Maintains the relative
  106. position of the cursor with respect to the calendar as well as possible."
  107.   (interactive "p")
  108.   (scroll-calendar-left (- arg)))
  109.  
  110. (defun scroll-calendar-left-three-months (arg)
  111.   "Scroll the displayed calendar window left by 3*ARG months.
  112. If ARG is negative the calendar is scrolled right.  Maintains the relative
  113. position of the cursor with respect to the calendar as well as possible."
  114.   (interactive "p")
  115.   (scroll-calendar-left (* 3 arg)))
  116.  
  117. (defun scroll-calendar-right-three-months (arg)
  118.   "Scroll the displayed calendar window right by 3*ARG months.
  119. If ARG is negative the calendar is scrolled left.  Maintains the relative
  120. position of the cursor with respect to the calendar as well as possible."
  121.   (interactive "p")
  122.   (scroll-calendar-left (* -3 arg)))
  123.  
  124. (defun calendar-cursor-to-nearest-date ()
  125.   "Move the cursor to the closest date.
  126. The position of the cursor is unchanged if it is already on a date.
  127. Returns the list (month day year) giving the cursor position."
  128.   (let ((date (calendar-cursor-to-date))
  129.         (column (current-column)))
  130.     (if date
  131.         date
  132.       (if (> 3 (count-lines (point-min) (point)))
  133.           (progn
  134.             (goto-line 3)
  135.             (move-to-column column)))
  136.       (if (not (looking-at "[0-9]"))
  137.           (if (and (not (looking-at " *$"))
  138.                    (or (< column 25)
  139.                        (and (> column 27)
  140.                             (< column 50))
  141.                        (and (> column 52)
  142.                             (< column 75))))
  143.               (progn
  144.                 (re-search-forward "[0-9]" nil t)
  145.                 (backward-char 1))
  146.             (re-search-backward "[0-9]" nil t)))
  147.       (calendar-cursor-to-date))))
  148.  
  149. (defun calendar-forward-day (arg)
  150.   "Move the cursor forward ARG days.
  151. Moves backward if ARG is negative."
  152.   (interactive "p")
  153.   (if (/= 0 arg)
  154.       (let*
  155.           ((cursor-date (calendar-cursor-to-date))
  156.            (cursor-date (if cursor-date
  157.                             cursor-date
  158.                           (if (> arg 0) (setq arg (1- arg)))
  159.                           (calendar-cursor-to-nearest-date)))
  160.            (new-cursor-date
  161.             (calendar-gregorian-from-absolute
  162.              (+ (calendar-absolute-from-gregorian cursor-date) arg)))
  163.            (new-display-month (extract-calendar-month new-cursor-date))
  164.            (new-display-year (extract-calendar-year new-cursor-date)))
  165.         ;; Put the new month on the screen, if needed, and go to the new date.
  166.         (if (not (calendar-date-is-visible-p new-cursor-date))
  167.             (calendar-other-month new-display-month new-display-year))
  168.         (calendar-cursor-to-visible-date new-cursor-date))))
  169.  
  170. (defun calendar-backward-day (arg)
  171.   "Move the cursor back ARG days.
  172. Moves forward if ARG is negative."
  173.   (interactive "p")
  174.   (calendar-forward-day (- arg)))
  175.  
  176. (defun calendar-forward-week (arg)
  177.   "Move the cursor forward ARG weeks.
  178. Moves backward if ARG is negative."
  179.   (interactive "p")
  180.   (calendar-forward-day (* arg 7)))
  181.  
  182. (defun calendar-backward-week (arg)
  183.   "Move the cursor back ARG weeks.
  184. Moves forward if ARG is negative."
  185.   (interactive "p")
  186.   (calendar-forward-day (* arg -7)))
  187.  
  188. (defun calendar-beginning-of-week (arg)
  189.   "Move the cursor back ARG calendar-week-start-day's."
  190.   (interactive "p")
  191.   (calendar-cursor-to-nearest-date)
  192.   (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
  193.     (calendar-backward-day
  194.      (if (= day calendar-week-start-day)
  195.          (* 7 arg)
  196.        (+ (mod (- day calendar-week-start-day) 7)
  197.           (* 7 (1- arg)))))))
  198.  
  199. (defun calendar-end-of-week (arg)
  200.   "Move the cursor forward ARG calendar-week-start-day+6's."
  201.   (interactive "p")
  202.   (calendar-cursor-to-nearest-date)
  203.   (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
  204.     (calendar-forward-day
  205.      (if (= day (mod (1- calendar-week-start-day) 7))
  206.          (* 7 arg)
  207.        (+ (- 6 (mod (- day calendar-w